package character.sprite.data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import character.sprite.model.CharacterStyle;

import configuration.Paths;
import data.DataTools;

/**
 * @deprecated
 * @author Richard
 * 
 */
public class CharacterStyleDatabase {

	private static CharacterStyleDatabase instance = null;

	private static final String MANIFEST_FILENAME = "style_manifest.txt";

	private HashMap<String, CharacterStyle> body;
	private HashMap<String, CharacterStyle> face;
	private HashMap<String, CharacterStyle> dress;
	private HashMap<String, CharacterStyle> hair;
	private HashMap<String, CharacterStyle> head;

	private CharacterStyleDatabase() {
		body = new HashMap<String, CharacterStyle>();
		face = new HashMap<String, CharacterStyle>();
		dress = new HashMap<String, CharacterStyle>();
		hair = new HashMap<String, CharacterStyle>();
		head = new HashMap<String, CharacterStyle>();
		this.updateFromFiles();
	}

	public static CharacterStyleDatabase getInstance() {
		if (instance == null) {
			instance = new CharacterStyleDatabase();
		}
		return instance;
	}

	public void updateFromFiles() {
		File charDir = new File(Paths.characterDataPath);
		File[] styleTypeDirs = new File[5];
		styleTypeDirs[0] = new File(charDir.getAbsolutePath() + File.separator
				+ "Body");
		styleTypeDirs[1] = new File(charDir.getAbsolutePath() + File.separator
				+ "Hair");
		styleTypeDirs[2] = new File(charDir.getAbsolutePath() + File.separator
				+ "Head");
		styleTypeDirs[3] = new File(charDir.getAbsolutePath() + File.separator
				+ "Face");
		styleTypeDirs[4] = new File(charDir.getAbsolutePath() + File.separator
				+ "Dress");
		for (File s : styleTypeDirs) {
			File[] pluginDirs = s.listFiles();
			for (File p : pluginDirs) {
				if (!p.getAbsolutePath().contains(".svn")) {
					CharacterStyle cs = this.buildStyleModel(p
							.getAbsolutePath());
					if (cs != null) {
						if (s.getName().trim().equals("Body")) {
							body.put(cs.getFormalName(), cs);
						} else if (s.getName().trim().equals("Hair")) {
							hair.put(cs.getFormalName(), cs);
						} else if (s.getName().trim().equals("Dress")) {
							dress.put(cs.getFormalName(), cs);
						} else if (s.getName().trim().equals("Head")) {
							head.put(cs.getFormalName(), cs);
						} else if (s.getName().trim().equals("Face")) {
							face.put(cs.getFormalName(), cs);
						}
					}
				}
			}
		}
	}

	private CharacterStyle buildStyleModel(String manifestPath) {
		if (!manifestPath.contains(".svn")) {
			try {
				File f = new File(manifestPath + File.separator
						+ MANIFEST_FILENAME);
				FileReader fr = new FileReader(f);
				BufferedReader reader = new BufferedReader(fr);
				String formalName = reader.readLine();
				String imagePath = reader.readLine();
				String type = reader.readLine();
				CharacterStyle cs = new CharacterStyle(DataTools
						.loadImage(manifestPath + File.separator + imagePath),
						formalName, type);
				reader.close();
				fr.close();
				return cs;
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public void importStylePlugin(String pluginDirPath) {
		CharacterStyle cs = this.buildStyleModel(pluginDirPath);
		if (cs != null) {
			if (cs.getType().equals("Body")) {
				body.put(cs.getFormalName(), cs);
			} else if (cs.getType().equals("Hair")) {
				hair.put(cs.getFormalName(), cs);
			} else if (cs.getType().equals("Dress")) {
				dress.put(cs.getFormalName(), cs);
			} else if (cs.getType().equals("Head")) {
				head.put(cs.getFormalName(), cs);
			} else if (cs.getType().equals("Face")) {
				face.put(cs.getFormalName(), cs);
			}
			File dir = new File(pluginDirPath);
			File outDir = new File(Paths.characterDataPath + File.separator
					+ cs.getType() + File.separator + dir.getName());
			try {
				if (!outDir.exists()) {
					outDir.mkdir();
				}
				File[] pluginFiles = dir.listFiles();
				for (File f : pluginFiles) {
					File out = new File(outDir.getAbsolutePath()
							+ File.separator + f.getName());
					if (!out.exists()) {
						out.createNewFile();
					}
					DataTools.copyFile(f, out);
				}
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	public ArrayList<String> getAvailableBodies() {
		ArrayList<String> ret = new ArrayList<String>(body.keySet());
		Collections.sort(ret);
		return ret;
	}

	public ArrayList<String> getAvailableHeads() {
		ArrayList<String> ret = new ArrayList<String>(head.keySet());
		Collections.sort(ret);
		return ret;
	}

	public ArrayList<String> getAvailableHair() {
		ArrayList<String> ret = new ArrayList<String>(hair.keySet());
		Collections.sort(ret);
		return ret;
	}

	public ArrayList<String> getAvailableDresses() {
		ArrayList<String> ret = new ArrayList<String>(dress.keySet());
		Collections.sort(ret);
		return ret;
	}

	public ArrayList<String> getAvailableFaces() {
		ArrayList<String> ret = new ArrayList<String>(face.keySet());
		Collections.sort(ret);
		return ret;
	}

	public CharacterStyle[] toStyleArray(String styleType, String[] styleNames) {
		CharacterStyle[] imageArray = new CharacterStyle[styleNames.length];
		for (int i = 0; i < styleNames.length; i++) {
			imageArray[i] = CharacterStyleDatabase.getInstance().getStyle(
					styleNames[i]);
		}
		return imageArray;
	}

	public CharacterStyle getStyle(String styleName) {
		if (body.keySet().contains(styleName)) {
			return body.get(styleName);
		} else if (hair.keySet().contains(styleName)) {
			return hair.get(styleName);
		} else if (head.keySet().contains(styleName)) {
			return head.get(styleName);
		} else if (face.keySet().contains(styleName)) {
			return face.get(styleName);
		} else if (dress.keySet().contains(styleName)) {
			return dress.get(styleName);
		} else {
			return null;
		}
	}

	public String getStyleType(String styleName) {
		if (body.keySet().contains(styleName)) {
			return "Body";
		} else if (hair.keySet().contains(styleName)) {
			return "Hair";
		} else if (head.keySet().contains(styleName)) {
			return "Head";
		} else if (face.keySet().contains(styleName)) {
			return "Face";
		} else if (dress.keySet().contains(styleName)) {
			return "Dress";
		} else {
			return null;
		}
	}

	public String getDefaultStyleForType(String styleType) {
		if (styleType.equals("Body")) {
			return "[[No Body]]";
		} else if (styleType.equals("Hair")) {
			return "[[No Hair]]";
		} else if (styleType.equals("Head")) {
			return "[[No Head]]";
		} else if (styleType.equals("Face")) {
			return "[[No Face]]";
		} else if (styleType.equals("Dress")) {
			return "[[No Dress]]";
		} else {
			return null;
		}
	}

	public void save() {
		// Performs no action since data is only loaded here not saved
	}

}
